import { message } from 'antd';
import {
  queryModelClass,
  queryModelClassTree,
  add,
  edit,
  del,
  qryById,
} from '@/services/modelclass';

export default {
  namespace: 'modelclass',

  state: {
    list: [],
    treeData: [],
    pagination: { pageSize: 10 },
    current: {},
    detail: {},
    editDetail: {},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryModelClass, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'save',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *tree({ payload }, { call, put }) {
      const response = yield call(queryModelClassTree, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveTree',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *add({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(add, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({ type: 'fetch' });
        yield put({ type: 'tree' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *del({ payload }, { call, put }) {
      const response = yield call(del, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({ type: 'fetch' });
        yield put({ type: 'tree' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *edit({ payload, form }, { call, put }) {
      const response = yield call(edit, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        yield put({ type: 'hideEditViewModal' });
        yield put({ type: 'fetch' });
        yield put({ type: 'tree' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *qryById({ payload }, { call, put }) {
      const response = yield call(qryById, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *showEditView({ payload }, { call, put }) {
      const response = yield call(qryById, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveEditDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload.data,
        pagination: {
          current: action.payload.current,
          pageSize: action.payload.size,
          total: action.payload.iTotalRecords,
        },
      };
    },
    saveTree(state, action) {
      return {
        ...state,
        treeData: action.payload.data,
      };
    },

    saveDetail(state, action) {
      return {
        ...state,
        detail: action.payload.data,
        viewModalVisible: true,
      };
    },
    saveEditDetail(state, action) {
      return {
        ...state,
        editDetail: action.payload.data,
        editViewModalVisible: true,
      };
    },
    hideViewModal(state) {
      return { ...state, viewModalVisible: false };
    },
    hideEditViewModal(state) {
      return { ...state, editViewModalVisible: false };
    },
  },
};
